package com.module.common.factory;

import com.module.dao.DictMapper;
import com.module.model.Dict;
import com.module.utils.SpringContextHolder;
import com.module.utils.ToolUtil;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author: Mr.Zhong
 * @create: 2018-06-30 15:05
 * @description:
 **/
@Component
@DependsOn("springContextHolder")
public class ConstantFactory implements IConstantFactory {

    private DictMapper dictMapper = SpringContextHolder.getBean(DictMapper.class);

    public static IConstantFactory me() {
        return SpringContextHolder.getBean("constantFactory");
    }

    /**
     * 查询字典
     */
    @Override
    public List<Dict> findInDict(Integer id) {
        if (ToolUtil.isEmpty(id)) {
            return null;
        } else {

            List<Dict> dicts = dictMapper.selectList(id);
            if (dicts == null || dicts.size() == 0) {
                return null;
            } else {
                return dicts;
            }
        }
    }

    @Override
    public String getStatusName(Integer status) {
        return getDictsByName("用户状态", status);
    }

    @Override
    public String getSex(Integer user_gender) {
        return getDictsByName("性别", user_gender);
    }

    @Override
    public String getOrderStatus(int order_status) {
        return getDictsByName("订单状态",order_status);
    }

    @Override
    public String getGoodState(Integer state) {
        return getDictsByName("商品状态", state);
    }
    /**
     * 根据字典名称和字典中的值获取对应的名称
     */
    public String getDictsByName(String name, Integer val) {
        Dict temp = new Dict();
        temp.setName(name);
        Dict dict = dictMapper.selectByName(temp);
        if (dict == null) {
            return "";
        } else {
            List<Dict> dicts = dictMapper.selectList(dict.getId());
            for (Dict item : dicts) {
                if (item.getNum() != null && item.getNum().equals(val)) {
                    return item.getName();
                }
            }
            return "";
        }
    }
}
